import Vue from "vue";
import VueRouter from 'vue-router'
import store from "../store";

Vue.use(VueRouter)
const Login = ()=> import('../view/Login')
const Home = () => import('../view/Home')
const Statistics = ()=> import('../view/home/Statistics')
const UserList =  ()=> import('../view/home/User')
const Category = ()=> import('../view/home/Category')
const Goods = ()=> import('../view/home/Goods')
const Comments =()=> import('../view/home/Comments')
const Order = ()=> import('../view/home/Order')
const Swiper = ()=> import('../view/home/Swiper')


const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}


const routes = [
    {
        path:'/login',
        name:'login',
        component:Login
     },
    {
        path:'/',
        redirect:'/home',
    },
    {
        path: "/home",
        name:'home',
        component:Home,
        children:[
            {
                path:'/home/statistics',
                name:'Statistics',
                component:Statistics
            },
            {
              path:'/home',
              redirect:'/home/statistics'
            },
            {
                path:'/home/userList',
                name:'UserList',
                component:UserList
            },
            {
                path:'categoryList',
                name:'Category',
                component:Category
            },
            {
                path:'goodsList',
                name:'Goods',
                component:Goods
            },
            {
                path:'reviewList',
                name:'Comments',
                component:Comments
            },
            {
                path:'orderList',
                name:'Order',
                component:Order
            },
            {
                path:'swiperList',
                name:'Swiper',
                component:Swiper
            }
        ]
    }

]

const router = new VueRouter({
    routes,
    mode:'history'
})

router.beforeEach((to,from,next) =>{
    if(to.path === '/login') next()
    const token = window.localStorage.getItem('token') || store.state.token
    if(!token && to.path !== '/login'){
        next({path:'/login'})
    }else {
        next()
    }
    // console.log(token);
})
export default  router